

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;

namespace JTC.MiniCommerce.Web.controls {
  public partial class ordersummary : System.Web.UI.UserControl {

    #region Member Variables

    private Order _order;
    private bool _isEditable = false;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load (object sender, EventArgs e) {
      try {
        if (this.Order != null) {
          SetOrderSummaryProperties();
          LoadOrder();
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(ordersummary).Name + ".Page_Load", ex);
        throw;
      }
    }

    /// <summary>
    /// Handles the Click event of the lbDelete control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void lbDelete_Click (object sender, CommandEventArgs e) {
      try {
        int orderItemId = 0;
        int.TryParse(e.CommandArgument.ToString(), out orderItemId);
        if (orderItemId > 0) {
          Order modifiedOrder = new OrderController().FetchOrder(WebUtility.GetUserName());
          new OrderController().RemoveItem(modifiedOrder.OrderId, orderItemId, WebUtility.GetUserName());
          if (modifiedOrder.OrderItemCollection.Count == 0) {
            Order.Delete(modifiedOrder.OrderId);
          }
        }
        Response.Redirect("~/cart.aspx");
      }
      catch (Exception ex) {
        Logger.Error(typeof(ordersummary).Name + ".lbDelete_Click", ex);
        throw;
      }
    }

    /// <summary>
    /// Handles the ItemDataBound event of the rptrCart control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.RepeaterItemEventArgs"/> instance containing the event data.</param>
    void rptrCart_ItemDataBound(object sender, RepeaterItemEventArgs e) {
      TextBox quantityTextBox = e.Item.FindControl("txtQuantity") as TextBox;
      if (quantityTextBox != null) {
        if (!this.IsEditable) {
          quantityTextBox.CssClass = "readOnly";
          quantityTextBox.ReadOnly = true;
        }
      }
    }


    #endregion

    #region Methods

    #region Public

    /// <summary>
    /// Loads the order.
    /// </summary>
    public void LoadOrder () {
      rptrCart.DataSource = this.Order.OrderItemCollection;
      rptrCart.ItemDataBound += new RepeaterItemEventHandler(rptrCart_ItemDataBound);
      rptrCart.DataBind();

      lblSubTotalAmount.Text = StoreUtility.GetFormattedAmount(this.Order.SubTotal, true);
      lblShippingAmount.Text = StoreUtility.GetFormattedAmount(this.Order.ShippingAmount, true);
      lblTaxAmount.Text = StoreUtility.GetFormattedAmount(this.Order.TaxAmount, true);
      if(this.Order.DiscountAmount > 0) {
        lblCouponAmount.Text = "-" + StoreUtility.GetFormattedAmount(this.Order.DiscountAmount, true);
      }
      else {
        lblCouponAmount.Text = StoreUtility.GetFormattedAmount(this.Order.DiscountAmount, true);
      }
      lblTotalAmount.Text = StoreUtility.GetFormattedAmount(this.Order.Total, true);
    }

    #endregion

    #region Protected

    /// <summary>
    /// Gets the formatted amount.
    /// </summary>
    /// <param name="pricePaid">The price paid.</param>
    /// <returns></returns>
    protected string GetFormattedAmount (string pricePaid) {
      return StoreUtility.GetFormattedAmount(pricePaid, true);
    }

    /// <summary>
    /// Gets the delete text.
    /// </summary>
    /// <returns></returns>
    protected string GetDeleteText () {
      return LocalizationUtility.GetText("lblDelete");
    }

    #endregion

    #region Private

    /// <summary>
    /// Sets the order summary properties.
    /// </summary>
    private void SetOrderSummaryProperties () {
      LocalizationUtility.AddLabelText(lblOrderSummary);
      LocalizationUtility.AddLabelText(lblItemSku);
      LocalizationUtility.AddLabelText(lblItem);
      LocalizationUtility.AddLabelText(lblQuantity);
      LocalizationUtility.AddLabelText(lblPrice);
      LocalizationUtility.AddLabelText(lblExtendedAmount);
      LocalizationUtility.AddLabelText(lblSubTotal);
      LocalizationUtility.AddLabelText(lblShipping);
      LocalizationUtility.AddLabelText(lblTax);
      LocalizationUtility.AddLabelText(lblCoupon);
      LocalizationUtility.AddLabelText(lblTotal);
    }

    #endregion

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the order.
    /// </summary>
    /// <value>The order.</value>
    public Order Order {
      get {
        return _order;
      }
      set {
        _order = value;
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public bool IsEditable {
      get {
        return _isEditable;
      }
      set {
        _isEditable = value;
      }
    }

    /// <summary>
    /// Gets the repeater.
    /// </summary>
    /// <value>The repeater.</value>
    public Repeater Repeater {
      get {
        return rptrCart;
      }
    }

    #endregion

  }
}